Skip to content

refactor(orgs): get organizations via GraphQL and display name instead login#347

Closed
lex111 wants to merge 5 commits into
masterfrom
refactor-get-orgs
Closed

refactor(orgs): get organizations via GraphQL and display name instead login#347
lex111 wants to merge 5 commits into
masterfrom
refactor-get-orgs

Conversation

@lex111
Copy link
Copy Markdown
Member

@lex111 lex111 commented Sep 20, 2017

There are currently two requests when receiving organizations: one requests public organizations, another request also requests organizations with private membership. Using GraphQL, we can get all organizations with one query. However, doing so required updating the scope of the token. We need to somehow come up with a way to update the token, how to do it better, who has any ideas?

Also, within the framework of this PR done that, the organization name is output instead of the login, this closes #272

Comment thread src/api/index.js Outdated
return response.json().then(data => {
const orgs = [];

data.data.user.organizations.edges.reduce((key, item) =>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I think that's not the best way of use reduce method, reduce returns something, this way you are just iterating over the array(similar what we do with forEach)

What do you think about this approach?

    return data.data.user.organizations.edges
        .map(item => item.node)
        .sort((org1, org2) => org1.name > org2.name)    

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jouderianjr I completely agree, thank you, I will fix it.

@lex111
Copy link
Copy Markdown
Member Author

lex111 commented Sep 20, 2017

There remains such a problem: we need to open a link to reauthorize, in order for the user to update the token. How is this best done?

Comment thread src/api/index.js Outdated

export async function fetchUserOrgs(user, accessToken) {
const ORGS_ENDPOINT = `${root}/users/${user}/orgs`;
const getOrganizationsQuery = `
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit scared of seeing api/index.js grow too much. Would it be possible to put queries in entities subfolders ? This one for example would go in user/user.queries.js

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Highly agree, I made the mistake of laying things all down in api/index.js but it really makes sense to leverage feature api files (like we do with other parts of the app).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving this query into something like

/src/queries/foo.query.js or something like that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better this way /src/api/queries/foo.query.js?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about /src/user/user.queries.js ? (just like we'll have user.api.js) ?

style={styles.avatar}
source={{
uri: user.avatar_url,
uri: imageUrl || user.avatar_url,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best to make the imageUrl prop mandatory and pass it as { user.avatar_url } where needed

Copy link
Copy Markdown
Member Author

@lex111 lex111 Sep 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that it will turn out, i.e. when querying GraphQL, another notation is used (lowerCase instead of snake_case). We have already done this here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think that having an optional prop, magically provided by another prop in some cases, is a bit confusing. I'm in favor of "real dumb" presentation components.

Also in the spirit of #350 (did you check it?), I think GraphQL responses should be stored using the same schemas as REST responses in the entities array. That would enable us to access the data in the same way, regardless of where it came from (rest or graphql). Am I making sense? :/

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not watched, but probably this will solve the problem, it seems to me...

Copy link
Copy Markdown
Member

@housseindjirdeh housseindjirdeh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm loving this @lex111, thank you. Left some discussion comments

Comment thread src/auth/screens/login.screen.js Outdated
signIn = () =>
openURLInView(
`https://github.com/login/oauth/authorize?response_type=token&client_id=${CLIENT_ID}&redirect_uri=gitpoint://welcome&scope=user%20repo&state=${stateRandom}`
`https://github.com/login/oauth/authorize?response_type=token&client_id=${CLIENT_ID}&redirect_uri=gitpoint://welcome&scope=user%20repo%20read:org&state=${stateRandom}`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this adds read access to org data. Without this, does your queries not work?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, without this, not getting an organization, there will be an error. Therefore, I write that there is a problem: we need to update the token when changing the scope, as for example in this case. And the question: how to do this?

Comment thread src/api/index.js Outdated

export async function fetchUserOrgs(user, accessToken) {
const ORGS_ENDPOINT = `${root}/users/${user}/orgs`;
const getOrganizationsQuery = `
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Highly agree, I made the mistake of laying things all down in api/index.js but it really makes sense to leverage feature api files (like we do with other parts of the app).

Comment thread src/api/index.js Outdated
id
login
name
avatarUrl
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seconding @machour's comment. I still don't know how exactly GraphQL works, but can we not stick to the same mention of avatar_url or does this version of the API just return it as avatarUrl instead of avatar_url?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just the point, that the GraphQL query returns the keys in another notation

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I think you can make an alias to what you get from GQL. I'd prefer camelCase instead of snake_case though :P

@machour
Copy link
Copy Markdown
Member

machour commented Sep 28, 2017

@lex111 For your changed scope and the need to re-authorize, I think this call should help:
https://developer.github.com/v3/oauth_authorizations/#check-an-authorization
You can see with this call if your needed permission is granted, or if you should ask the user to grant it

<WebView
source={{
uri: `https://github.com/login/oauth/authorize?response_type=token&client_id=${CLIENT_ID}&redirect_uri=gitpoint://welcome&scope=user%20repo&state=${stateRandom}`,
uri: `https://github.com/login/oauth/authorize?response_type=token&client_id=${CLIENT_ID}&redirect_uri=gitpoint://welcome&scope=user%20repo%20read:org&state=${stateRandom}`,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for later but I think we can move this url into a constants files.

@machour
Copy link
Copy Markdown
Member

machour commented Apr 23, 2018

Closing this PR as it's superseded by the ongoing store refactoring. #272 remains open and will be dealt with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use organisation Display name instead of organization name

6 participants